package com.bnyy.common.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.function.Consumer;

public abstract class BaseNormalListAdapter<T, VH extends BaseNormalListAdapter.ViewHolder> extends RecyclerView.Adapter<VH> {
    private ArrayList<T> datas = new ArrayList<>();

    public Context mContext;
    public LayoutInflater inflater;
    public OnItemClickListener<T> onItemClickListener;
    public Consumer<T> consumer;

    @Override
    public void onBindViewHolder(@NonNull VH holder, final int position) {

        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (consumer != null) {
                    consumer.accept(getData(position));
                }
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(view, getData(position), position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return getDatas().size();
    }

    public BaseNormalListAdapter(Context context) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    public BaseNormalListAdapter(Context context, OnItemClickListener<T> onItemViewClickListener) {
        this(context);
        this.onItemClickListener = onItemViewClickListener;
    }

    public BaseNormalListAdapter(Context context, Consumer<T> consumer) {
        this(context);
        this.consumer = consumer;
    }

    public void clear() {
        getDatas().clear();
        notifyDataSetChanged();
    }

    public void refresh(ArrayList<? extends T> datas) {
        getDatas().clear();
        getDatas().addAll(datas);
        notifyDataSetChanged();
    }

    public void loadMore(T data) {
        getDatas().add(data);
        notifyItemInserted(datas.size() - 1);
    }

    public void insert(T data, int position) {
        getDatas().add(position, data);
        notifyItemInserted(position);
    }

    public void insert(ArrayList<? extends T> datas, int position) {
        getDatas().addAll(position, datas);
        notifyItemRangeInserted(position, datas.size());
    }

    public void loadMore(ArrayList<? extends T> datas) {
        getDatas().addAll(datas);
        notifyItemRangeChanged(this.datas.size() - datas.size(), this.datas.size());
    }

    public void remove(int position) {
        getDatas().remove(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public synchronized ArrayList<T> getDatas() {
        return datas;
    }

    public T getData(int index) {
        return datas.get(index);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View root;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            root = itemView;
        }
    }

    public interface OnItemClickListener<T> {
        void onClick(View view, T t, int position);
    }
}

